from django.shortcuts import render
from django.views import View
from django.http import HttpResponse
from django.db import DatabaseError
from users.models import User
from django.shortcuts import redirect,reverse


class RegisterView(View):
    def get(self, requests):
        return render(requests,'register.html')

    def post(self, request):
        '''
        接收前端的参数

        '''
        print(request.POST)
        username = request.POST.get('username')
        password = request.POST.get('password')
        password2 = request.POST.get('password2')
        mobile = request.POST.get('mobile')
        allow = request.POST.get('allow')

        try:
            user = User.objects.create_user(username=username, password=password, mobile=mobile)
        except DatabaseError:  # 如果链接数据库失败
            return render(request, 'register.html', {'register_errmsg': '注册失败'})

        # 响应注册结果
        return redirect(reverse('contents:index'))